package com.uniledger.exchange.mine;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;
import com.uniledger.exchange.home.LineViewHolder;
import com.uniledger.exchange.login.Admin;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class MyCenterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements FlexibleDividerDecoration.VisibilityProvider {


    List<MyCenter> centerList;
    Context context;
    LayoutInflater inflater;


    public MyCenterAdapter(Context context) {
        this.context = context;
        this.centerList = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
    }

    public void addData() {
        centerList.add(new MyCenter("Alice", "15102045317", R.drawable.mask));//用户信息
        centerList.add(new MyCenter(getStr(R.string.me_scheme_bill), R.drawable.ic_mine_history));
        centerList.add(new MyCenter(getStr(R.string.me_scheme_friend), R.drawable.ic_mine_friend));
        centerList.add(new MyCenter(getStr(R.string.me_scheme_cards), R.drawable.ic_mine_cards));
//        centerList.add(new MyCenter("Help Center", R.drawable.ic_mine_help));
        centerList.add(new MyCenter(getStr(R.string.me_scheme_settings), R.drawable.ic_mine_setting));
        notifyDataSetChanged();
    }

    private String getStr(int id){
        return context.getResources().getString(id);
    }


    public void updateUserInfo(Admin admin) {
        MyCenter center = centerList.get(0);
        center.name = admin.firstName;
        center.tel = admin.mobile;
        center.avatar = admin.avatar;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        MyCenter center = centerList.get(position);
        return center.type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        if (type == MyCenter.TYPE_ME_USER) {
            View view = inflater.inflate(R.layout.item_mine_center_info, viewGroup, false);
            return new MineViewHolder(view);
        } else if (type == MyCenter.TYPE_ME_SCHEME) {
            View view = inflater.inflate(R.layout.item_mine_center_scheme, viewGroup, false);
            return new SchemeViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.item_home_center_line, viewGroup, false);
            return new LineViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        int type = getItemViewType(i);
        MyCenter center = centerList.get(i);

        if (type == MyCenter.TYPE_ME_USER) {//个人中心
            MineViewHolder holder = (MineViewHolder) viewHolder;
            holder.userName.setText(center.name);
            holder.userTel.setText(center.tel);
            GlideApp.with(context)
                    .load(center.avatar)
                    .placeholder(R.drawable.head)
                    .error(R.drawable.head)
                    .into(holder.userIcon);
        }

        if (type == MyCenter.TYPE_ME_SCHEME) {//分类跳转
            SchemeViewHolder holder = (SchemeViewHolder) viewHolder;
            holder.schemeName.setText(center.name);
            holder.schemeArrow.setImageResource(center.arrow);
            holder.schemeIcon.setImageResource(center.icon);
        }

    }

    @Override
    public int getItemCount() {
        return centerList.size();
    }

    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
        if (position == 0) {
            return true;
        }
        return false;
    }

    static class MineViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_user_icon)
        CircleImageView userIcon;
        @BindView(R.id.txt_user_name)
        TextView userName;
        @BindView(R.id.txt_user_tel)
        TextView userTel;

        public MineViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class SchemeViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_scheme_icon)
        ImageView schemeIcon;
        @BindView(R.id.txt_scheme_name)
        TextView schemeName;
        @BindView(R.id.img_scheme_arrow)
        ImageView schemeArrow;

        public SchemeViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
